package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"hotel/model/mysql"

	"hotel/hotelrpc/hotelrpc"
	"hotel/hotelrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *hotelrpc.UpdateRequest) (*hotelrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	var hotel mysql.Hotel
	mysql.DB.Debug().Where("id=?", in.Id).Find(&hotel)
	if hotel.ID == 0 {
		return nil, fmt.Errorf("用户未注册,请前往注册")
	}
	m := mysql.Hotel{
		Model:    gorm.Model{ID: uint(in.Id)},
		Image:    in.Image,
		VipName:  in.VipName,
		Sex:      in.Sex,
		Phone:    in.Phone,
		Person:   in.Person,
		Password: in.Password,
	}
	mysql.DB.Debug().Updates(&m)
	return &hotelrpc.UpdateResponse{Id: int64(m.ID)}, nil
}
